﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using SST.Phone.ViewModels;

using System.Windows.Data;
using SST.Phone.Util;

namespace SST.Phone.View
{
    public partial class SettingPage : PhoneApplicationPage
    {
        private SettingViewModel vm = new SettingViewModel();

        public SettingPage()
        {
            InitializeComponent();

            InitDataContext();
        }

        private void InitDataContext()
        {
            vm.Load();
            DataContext = vm;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            vm.Save();
            NavigationService.GoBack();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        private void btnClearLogin_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Delete login and password?", "Delete Login", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                vm.LoginNickname = null;
                vm.LoginPassword = null;
                vm.Save();
                MessageBox.Show("Login information deleted");
            }
        }

        private void txt_TextChanged(object sender, TextChangedEventArgs e)
        {
            //http://thegrayzone.co.uk/blog/2010/10/update-silverlight-textbox-binding-source-on-change/
            BindingExpression binding = (sender as TextBox).GetBindingExpression(TextBox.TextProperty);
            binding.UpdateSource();
        }

        private void txtPassword_PasswordChanged(object sender, RoutedEventArgs e)
        {
            BindingExpression binding = (sender as PasswordBox).GetBindingExpression(PasswordBox.PasswordProperty);
            binding.UpdateSource();
        }

        private void listChatRefresh_Loaded(object sender, RoutedEventArgs e)
        {
            Binding pickerBinding = new Binding("ChatRefreshSeconds")
            {
                Source = vm,
                Mode = BindingMode.TwoWay,
                Converter = new UIChatRefreshTimeConverter()
            };
            listChatRefresh.SetBinding(ListPicker.SelectedIndexProperty, pickerBinding);
        }

        private void chkSystemTray_Loaded(object sender, RoutedEventArgs e)
        {
            Binding chkBinding = new Binding("ShowTray")
            {
                Source = vm,
                Mode = BindingMode.TwoWay
            };
            chkSystemTray.SetBinding(ToggleSwitch.IsCheckedProperty, chkBinding);
        }

        private void chkSystemTray_Checked(object sender, RoutedEventArgs e)
        {
            chkSystemTray.Content = "Show";
        }

        private void chkSystemTray_Unchecked(object sender, RoutedEventArgs e)
        {
            chkSystemTray.Content = "Hide";
        }
    }
}